package org.jivesoftware.smackx;

import java.util.ArrayList;
import java.util.List;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.filter.PacketExtensionFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.packet.PEPEvent;
import org.jivesoftware.smackx.packet.PEPItem;
import org.jivesoftware.smackx.packet.PEPPubSub;

public class PEPManager
{
  private Connection connection;
  private PacketFilter packetFilter;
  private PacketListener packetListener;
  private List<PEPListener> pepListeners = new ArrayList();

  public PEPManager(Connection paramConnection)
  {
    this.packetFilter = new PacketExtensionFilter("event", "http://jabber.org/protocol/pubsub#event");
    this.connection = paramConnection;
    init();
  }

  // ERROR //
  private void firePEPListeners(java.lang.String paramString, PEPEvent paramPEPEvent)
  {
    // Byte code:
    //   0: aconst_null
    //   1: checkcast 49	[Lorg/jivesoftware/smackx/PEPListener;
    //   4: pop
    //   5: aload_0
    //   6: getfield 25	org/jivesoftware/smackx/PEPManager:pepListeners	Ljava/util/List;
    //   9: astore 4
    //   11: aload 4
    //   13: monitorenter
    //   14: aload_0
    //   15: getfield 25	org/jivesoftware/smackx/PEPManager:pepListeners	Ljava/util/List;
    //   18: invokeinterface 55 1 0
    //   23: anewarray 57	org/jivesoftware/smackx/PEPListener
    //   26: astore 6
    //   28: aload_0
    //   29: getfield 25	org/jivesoftware/smackx/PEPManager:pepListeners	Ljava/util/List;
    //   32: aload 6
    //   34: invokeinterface 61 2 0
    //   39: pop
    //   40: aload 4
    //   42: monitorexit
    //   43: iconst_0
    //   44: istore 8
    //   46: iload 8
    //   48: aload 6
    //   50: arraylength
    //   51: if_icmplt +12 -> 63
    //   54: return
    //   55: astore 5
    //   57: aload 4
    //   59: monitorexit
    //   60: aload 5
    //   62: athrow
    //   63: aload 6
    //   65: iload 8
    //   67: aaload
    //   68: aload_1
    //   69: aload_2
    //   70: invokeinterface 64 3 0
    //   75: iinc 8 1
    //   78: goto -32 -> 46
    //
    // Exception table:
    //   from	to	target	type
    //   14	43	55	finally
    //   57	60	55	finally
  }

  private void init()
  {
    this.packetListener = new PacketListener(this)
    {
      public void processPacket()
      {
        Message localMessage = (Message)paramPacket;
        PEPEvent localPEPEvent = (PEPEvent)localMessage.getExtension("event", "http://jabber.org/protocol/pubsub#event");
        PEPManager.access$0(this.this$0, localMessage.getFrom(), localPEPEvent);
      }
    };
    this.connection.addPacketListener(this.packetListener, this.packetFilter);
  }

  public void addPEPListener(PEPListener paramPEPListener)
  {
    synchronized (this.pepListeners)
    {
      if (!(this.pepListeners.contains(paramPEPListener)))
        this.pepListeners.add(paramPEPListener);
      return;
    }
  }

  public void destroy()
  {
    if (this.connection != null)
      this.connection.removePacketListener(this.packetListener);
  }

  protected void finalize()
    throws Throwable
  {
    destroy();
    super.finalize();
  }

  public void publish(PEPItem paramPEPItem)
  {
    PEPPubSub localPEPPubSub = new PEPPubSub(paramPEPItem);
    localPEPPubSub.setType(IQ.Type.SET);
    this.connection.sendPacket(localPEPPubSub);
  }

  public void removePEPListener(PEPListener paramPEPListener)
  {
    synchronized (this.pepListeners)
    {
      this.pepListeners.remove(paramPEPListener);
      return;
    }
  }
}